//
//  ZJDeliveryAddressViewController.m
//  ZiJinLian
//
//  Created by lzy on 2017/3/17.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "ZJDeliveryAddressViewController.h"
#import "ZJAddressCell.h"
#import "ZJAddAddressViewController.h"
#import "ZJManageAddressController.h"
#import  "Masonry.h"
@interface ZJDeliveryAddressViewController ()

@end

@implementation ZJDeliveryAddressViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden=NO;
}


-(void)createView{
    [self initTitle:MyString(@"地址列表")];
       [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"home_nav_bg"] forBarMetrics:UIBarMetricsDefault];
    [ZJNavTool rightItemOnNavigationItem:self target:self action:@selector(managerAddress) andType:item_manage];
    //表
    self.tableView = [UITableView tableView];
    self.tableView.delegate =self;
    self.tableView.dataSource =self;
    self.tableView.sectionFooterHeight=10;
    self.tableView.frame=CGRectMake(0, 0,MainW, MainH-64-50);
    [self.view addSubview:self.tableView];
    //按钮
    _addAddressBtn=[UIButton creatBtn];
    [_addAddressBtn ZJButtonStyleWithBgImg:@"icon_plus" withTitle:MyString(@"新增地址")];
    [self.view addSubview:_addAddressBtn];
    [_addAddressBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.height.mas_equalTo(50);
        make.bottom.equalTo(self.view.mas_bottom);
    }];

}
-(void)doOpeartion{
    [_addAddressBtn addTarget:self action:@selector(addAddress) forControlEvents:UIControlEventTouchUpInside];

}
#pragma mark 管理地址
-(void)managerAddress{
    if (_data.count==0) {
        [self showMessage:MyString(@"请先添加地址")];
        return;
    }
    ZJManageAddressController* manage =[[ZJManageAddressController alloc]init];
    manage.data=[[NSMutableArray alloc]init];
    [manage.data addObjectsFromArray:_data];
    manage.reflesh=^(ZJAdressModel* model){

        [self doRequest];
    };
    [self.navigationController pushViewController:manage animated:YES];
}


-(void)addAddress{
    if (self.data.count>=5) {
        [self showMessage:MyString(@"地址不能超过5个哦,管理下你的地址吧")];
        return;
    }
    
    ZJAddAddressViewController* controller =[[ZJAddAddressViewController alloc]init];
     __weak typeof(self) mySelf = self;
    controller.addData = ^(ZJAdressModel *model){
  
        [mySelf doRequest];
    };
    [self.navigationController pushViewController:controller animated:YES];
}




-(void)doRequest
{
     NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    [parametersDic setObject:[ZJUserDefaultsTools getCurrentUserID] forKey:@"userId"];
   [self doHttpRequestWithApi:API_ADDRESS_LIST method:GET dic:parametersDic successBlock:^(NSDictionary *dic) {
       //获得数组 遍历字典
       NSArray *array=dic[@"data"];
       if (array.count==0) {
           //数组为空 显示空页面
           [ZJloadView showNoResultView:self.tableView withimageName:@"img_blank_monk" withNoResult:MyString(@"您现在还没有填写地址哟")];
       }else
       {           
           if (self.data.count>0) {
               [self.data removeAllObjects];
           }
           [ZJloadView hideNoResultView:self.tableView];
           for (NSDictionary *addressDic in array) {
               ZJAdressModel *model = [ZJAdressModel mj_objectWithKeyValues:addressDic];
               [self.data addObject:model];
           }
         
           [self.tableView reloadData];
       }
     
   }];

}





- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return  self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ZJAdressCellID = @"ZJAdressCell";
    ZJAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:ZJAdressCellID];
    if (cell == nil) {
        cell = [[ZJAddressCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ZJAdressCellID];
        cell.backgroundColor = [UIColor whiteColor];
    }
    
    if (self.data[indexPath.section]) {
        [cell setUpCellViewAndModel:self.data[indexPath.section]];
    }

    return cell;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
    
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
       UIView *secView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MainW, 10)];
       secView.backgroundColor = [UIColor clearColor];
        return secView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.selected = NO;
    if (_styple==1) {
        ZJAdressModel*modle=self.data[indexPath.section];
        self.addressBlock(modle);
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView=[[UITableView alloc]init];
    }
    return _tableView;
}
-(NSMutableArray *)data{
    if (!_data) {
        _data=[[NSMutableArray alloc]init];
    }
    return _data;
}

@end
